package self.zhangkang.chart.view.kline.index

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import self.zhangkang.chart.data.KLineInfo
import self.zhangkang.chart.tools.CanvasTools
import self.zhangkang.chart.tools.NumberFormatUtils
import self.zhangkang.chart.view.IndexNameConfig
import self.zhangkang.chart.view.kline.KLineIndex
import self.zhangkang.chart.view.kline.KLineIndexInfo
import self.zhangkang.chart.view.kline.KLineViewColor
import self.zhangkang.chart.view.kline.KLineViewDimen
import self.zhangkang.chart.view.pool.PathPool
import self.zhangkang.chart.view.pool.RectPool

/**
 * @author : Android-张康
 * created on: 2023/5/8 16:14
 * description: K线中 RSI 绘制
 */
internal class KLineCCI(
    private val pathPool: PathPool,
    private val rectPool: RectPool,
    private val kLineViewColor: KLineViewColor,
    private val kLineViewDimen: KLineViewDimen,
    private val indexNameConfig: IndexNameConfig
) : KLineIndexInfo {

    private var mMaxValue: Float = 0.0F

    private var mMinValue: Float = 0.0F

    private var priceItem: Float = 0.0F

    /**
     * 分时的路径
     */
    private var mCCIPath = Path()

    /**
     * 是否可以切换
     *
     * @return true 可以切换，false 不可以切换
     */
    override fun canSwitch(): Boolean {
        return getIndexInfo().canSwitch
    }

    /**
     * 是否点击到名称
     *
     * @param startX 开始X
     * @param startY 开始Y
     * @param stopX 结束X
     * @param stopY 结束Y
     * @param touchX 触摸的 X
     * @param touchY 触摸的 Y
     * @param textPaint 文字的画笔
     *
     * @return true 是，false 否
     */
    override fun isClickInName(
        startX: Float,
        startY: Float,
        stopX: Float,
        stopY: Float,
        touchX: Float,
        touchY: Float,
        textPaint: Paint
    ): Boolean {
        val textBoundsRect = rectPool.acquire()
        //先绘制指标名称
        CanvasTools.getDrawTextBounds(
            startX = startX,
            startY = startY,
            stopY = stopY,
            textPaint = textPaint,
            indicatorName = getIndexInfo().indexName,
            indexNameConfig = indexNameConfig,
            textBoundsRect = textBoundsRect,
            hasArrowDropDown = canSwitch(),
        )
        val contains = textBoundsRect.contains(touchX.toInt(), touchY.toInt())
        rectPool.release(textBoundsRect)
        return contains
    }

    /**
     * 获取指标类型
     */
    override fun getIndexInfo(): KLineIndex {
        return KLineIndex.CCI
    }

    override fun getProportion(): Int {
        return 15
    }

    override fun calculationMaxAndMinBefore(kLineInfo: KLineInfo) {
        mMaxValue = Float.MIN_VALUE
        mMinValue = Float.MAX_VALUE
    }

    override fun calculationMaxAndMin(kLineInfo: KLineInfo) {
        var maxValue = mMaxValue
        var minValue = mMinValue
        if (kLineInfo.cci.cci > maxValue) {
            maxValue = kLineInfo.cci.cci
        }
        if (kLineInfo.cci.cci < minValue) {
            minValue = kLineInfo.cci.cci
        }
        mMaxValue = maxValue
        mMinValue = minValue
    }

    override fun calculationMaxAndMinAfter() {

    }

    /**
     * 绘制背景
     *
     * @param canvas 画布
     * @param startX 开始X
     * @param startY 开始Y
     * @param stopX 结束X
     * @param stopY 结束Y
     * @param linePaint 画布
     */
    override fun drawBackground(
        canvas: Canvas,
        startX: Float,
        startY: Float,
        stopX: Float,
        stopY: Float,
        linePaint: Paint,
        textPaint: Paint
    ) {
        //绘制背景
        val backgroundPath = pathPool.acquire().apply {
            moveTo(startX, startY)
            lineTo(stopX, startY)
            lineTo(stopX, stopY)
            lineTo(startX, stopY)
            close()
        }
        linePaint.color = kLineViewColor.borderLineLineColor
        canvas.drawPath(backgroundPath, linePaint)
        pathPool.release(backgroundPath)

        val maxValue: Float = mMaxValue
        val minValue: Float = mMinValue
        //绘制最高价
        textPaint.textSize = kLineViewDimen.ordinateTextSize
        textPaint.color = kLineViewColor.ordinateTextColor
        val maxValueText = NumberFormatUtils.priceNumberFormat(maxValue)
        val textBoundsRect = rectPool.acquire()
        textPaint.getTextBounds(maxValueText, 0, maxValueText.length, textBoundsRect)
        val textStartY = textBoundsRect.height() + kLineViewDimen.textPaddingLine + startY
        canvas.drawText(
            maxValueText,
            startX + kLineViewDimen.textPaddingLine,
            textStartY,
            textPaint
        )
        //绘制最低价格
        val minValueText = NumberFormatUtils.priceNumberFormat(minValue)
        canvas.drawText(
            minValueText,
            startX + kLineViewDimen.textPaddingLine,
            stopY - kLineViewDimen.textPaddingLine,
            textPaint
        )
        rectPool.release(textBoundsRect)
    }

    override fun drawCurrentIndexInfo(
        canvas: Canvas,
        startX: Float,
        startY: Float,
        stopX: Float,
        stopY: Float,
        linePaint: Paint,
        textPaint: Paint,
        marginTop: Float,
        span: Int,
        indexValueHorizontalSpace: Float,
        indexValueVerticalSpace: Float,
        kLineInfo: KLineInfo,
        kLineIsLine: Boolean
    ) {
        val indicatorNameWidth = CanvasTools.drawIndicatorName(
            canvas = canvas,
            startX = startX,
            startY = startY,
            stopY = stopY,
            linePaint = linePaint,
            textPaint = textPaint,
            rectPool = rectPool,
            indicatorName = getIndexInfo().indexName,
            indexNameConfig = indexNameConfig,
            hasArrowDropDown = canSwitch()
        )

        textPaint.textSize = kLineViewDimen.indexValueTextSize
        textPaint.color = kLineViewColor.cciColor
        val indicatorStart = indicatorNameWidth + indexValueHorizontalSpace
        val text = "CCI14：" + NumberFormatUtils.priceNumberFormat(kLineInfo.cci.cci)
        CanvasTools.drawTextInVerticalCenter(
            canvas,
            indicatorStart,
            startY,
            stopY,
            text,
            textPaint,
            rectPool = rectPool
        )
    }

    override fun beforeDrawItemContent(
        canvas: Canvas,
        startX: Float,
        startY: Float,
        stopX: Float,
        stopY: Float,
        paint: Paint,
        candlestickWidth: Float,
        candlestickWidthSpace: Float,
        kLineInfo: KLineInfo,
        kLineIsLine: Boolean
    ) {
        val volumeRange = mMaxValue - mMinValue
        val drawHeight = stopY - startY
        priceItem = drawHeight / volumeRange
        mCCIPath = pathPool.acquire()
    }

    override fun drawItemContent(
        canvas: Canvas,
        startX: Float,
        startY: Float,
        stopX: Float,
        stopY: Float,
        textPaint: Paint,
        linePaint: Paint,
        kLineInfo: KLineInfo,
        isFirst: Boolean,
        yangLineIsFill: Boolean,
        totalTranslateX: Float,
        candlestickWidth: Float,
        halfCandlestickWidth: Float,
        kLineIsLine: Boolean
    ) {
        //Path 的横坐标
        val pathX = halfCandlestickWidth + totalTranslateX
        val indexValue = kLineInfo.cci.cci
        val indexY = priceItem * (mMaxValue - indexValue) + startY
        val path = mCCIPath
        if (isFirst) {
            if (indexY <= stopY) {
                path.moveTo(pathX, indexY)
            } else {
                path.moveTo(pathX, stopY)
            }
        } else {
            if (indexY < stopY) {
                path.lineTo(pathX, indexY)
            }
        }
    }

    override fun afterDrawItemContent(canvas: Canvas, paint: Paint) {
        paint.strokeWidth = kLineViewDimen.indexLineStrokeWidth
        val maColor = kLineViewColor.cciColor
        val maPath = mCCIPath
        paint.color = maColor
        canvas.drawPath(maPath, paint)
        pathPool.release(maPath)
    }

    /**
     * 获取当前左边的值
     *
     * @param percentage 比例
     *
     * @return 返回当前左边坐标的值
     */
    override fun getCurrentLeftValue(percentage: Float): String? {
        return null
    }

    /**
     * 获取当前右边的值
     *
     * @param percentage 比例
     *
     * @return 返回当前右边坐标的值
     */
    override fun getCurrentRightValue(percentage: Float): String? {
        return null
    }
}